package com.etc.emsys.controller.emp;

import com.etc.emsys.controller.BaseMenuController;
import com.etc.emsys.dao.emp.DeptDao;
import com.etc.emsys.dao.emp.EmpDao;
import com.etc.emsys.domain.DeptPojo;
import com.etc.emsys.domain.EmpPojo;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * 更新控制器
 * create by mazheng
 */
public class EmpUpdateController extends BaseMenuController {


    @FXML
    TextField empno;
    @FXML
    TextField ename;
    @FXML
    ComboBox deptcombox;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        DeptDao dao = new DeptDao();
        List<DeptPojo> list = dao.selectAllDept();
        deptcombox.setItems(FXCollections.observableList(list));
        EmpPojo emp = (EmpPojo) param;
        deptcombox.setValue(emp.getDname());
        empno.setText(String.valueOf(emp.getEmpno()));
        empno.setDisable(true);
        ename.setText(emp.getEname());
    }
    public void reset() {
        this.initialize(null, null);
    }

    public void update() {
        EmpPojo emp = (EmpPojo) param;

        if (ename.getText().equals(emp.getEname()) && deptcombox.getValue().equals(emp.getDname())
        ) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION, "没有变化");
            alert.showAndWait();
            return;
        } else {
            EmpPojo input = new EmpPojo();
            input.setEmpno(emp.getEmpno());
            input.setEname(ename.getText());
            input.setDname(String.valueOf(deptcombox.getValue()));

            EmpDao dao = new EmpDao();
            dao.updateEmp(input);
            Alert alert = new Alert(Alert.AlertType.INFORMATION, "更新成功");
            alert.showAndWait();

            transformNon("../../view/emp/main.fxml", "更新成功", null, ename.getScene());
        }
    }
}
